package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.R;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Dish;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.impl.CategoryServiceImpl;
import com.example.reggie.service.impl.DishServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.expression.Ids;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryServiceImpl categoryService;

    @Autowired
    DishServiceImpl dishService;

    /**
     * 新增分类
     * @param
     */
    @PostMapping()
    public R<String> save(@RequestBody Category categoryin){
        log.info("新增 type:{} 名字:{} 排序值:{} ",categoryin.getType(),categoryin.getName(),categoryin.getSort());
        categoryService.save(categoryin);
        return R.success("新增成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<IPage> page(int page, int pageSize){
        //分页构造器
        IPage<Category> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序,升序
        queryWrapper.orderByAsc(Category::getSort);
        queryWrapper.select(Category::getId,Category::getType,Category::getName,Category::getSort,
                Category::getCreateTime,Category::getUpdateTime,Category::getCreateUser,Category::getUpdateUser);
        //分页查询
        categoryService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据id删除分类
     * @param ids
     * @return
     */
    @DeleteMapping()
    public R<String> delete(@RequestParam Long ids){
        log.info("删除分类，id为：{}",ids);
        Category category = categoryService.getById(ids);
        //如果是套餐则直接删除
        if (category.getType()==2){
            return categoryService.removeById(ids)?R.success("分类信息删除成功"):R.error("删除失败");
        }
        //是菜品
        LambdaQueryWrapper<Dish>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,ids);
        List<Dish> list = dishService.list(queryWrapper);
        if (list==null){
            categoryService.removeById(ids);
            return categoryService.removeById(ids)?R.success("分类信息删除成功"):R.error("分类信息删除失败");
        }
        return R.error("分类信息删除失败");
    }


    /**
     * 根据id修改分类信息
     * @param category
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类信息：{}",category);

        categoryService.updateById(category);

        return R.success("修改分类信息成功");
    }

    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list( Category category){
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }

}
